<template>
  <div v-loading="loading">
    <div class="header-search cad-box">
      <el-input
        v-model="searchValue.realName"
        style="width: 20%"
        clearable
        placeholder="请输入计划名称"
      />
      <el-button
        type="primary"
        style="margin: 0px 10px"
        @click="handleGovCapitalSubsidyProjectList"
      >搜 索</el-button>
    </div>

    <div class="content-table cad-box">
      <el-table :data="tableData" style="width: 100%">
        <!-- <el-table-column label="序号" type="index" align="center" /> -->
        <el-table-column prop="name" label="计划名称" align="center" />
        <el-table-column prop="type" label="补贴类型" align="center">
          <template slot-scope="scope">
            {{
              scope.row.type !== null
                ? options2.find((item) => item.value === scope.row.type).label
                : "-"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="endTime" label="申请截止时间" align="center" />
        <el-table-column
          prop="applicationStatus"
          label="申请状态"
          align="center"
        >
          <template slot-scope="scope">
            {{
              scope.row.applicationStatus !== null
                ? options1.find(
                  (item) => item.value === scope.row.applicationStatus
                ).label
                : "-"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="fundsStatus" label="拨款状态" align="center">
          <template slot-scope="scope">
            {{
              scope.row.fundsStatus !== null
                ? options3.find((item) => item.value === scope.row.fundsStatus)
                  .label
                : "-"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleInfo(scope.row, 1)"
            >详情</el-button>
            <el-button
              v-if="scope.row.applicationStatus !== 1"
              type="text"
              size="small"
              @click="handleInfo(scope.row, 2)"
            >申请补贴</el-button>
            <el-button
              v-if="scope.row.isCXShow && scope.row.applicationStatus === 1"
              type="text"
              size="small"
              @click="handleGovCapitalSubsidyProjectRevoke(scope.row.id)"
            >撤销</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: right">
        <el-pagination
          :current-page="pagination.pageNum"
          :page-sizes="[10, 15, 30, 50]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-drawer title="详情" :visible.sync="drawer" :direction="direction">
      <div class="drawer_box">
        <div class="drawer_item">标题：{{ infoItem.name }}</div>
        <div class="drawer_item" style="justify-content: space-between">
          <div>类型：{{ infoItem.name }}</div>
          <div style="margin-left: 50px">分类：{{ infoItem.name }}</div>
        </div>
        <div class="drawer_item">备注：{{ infoItem.name }}</div>
        <div class="drawer_item">文件：{{ infoItem.name }}</div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {
  govCapitalSubsidyProjectList,
  govCapitalSubsidyProjectRevoke
} from './api'
export default {
  data () {
    return {
      options1: [
        {
          value: 0,
          label: '未申请'
        },
        {
          value: 1,
          label: '已申请'
        },
        {
          value: 2,
          label: '已撤销'
        },
        {
          value: 3,
          label: '已截止'
        }
      ],
      options2: [
        {
          value: 0,
          label: '建设补贴计划'
        },
        {
          value: 1,
          label: '运营补贴计划'
        }
      ],
      options3: [
        {
          value: 0,
          label: '未拨付'
        },
        {
          value: 1,
          label: '已拨付'
        }
      ],
      value1: '',
      value2: '',
      drawer: false,
      direction: 'rtl',
      infoItem: {},
      loading: false,
      searchValue: {
        realName: '',
        status: ''
      },
      tableData: [],
      pagination: {
        pageNum: 0,
        pageSize: 10
      },
      total: 0
    }
  },
  computed: {
    ...mapGetters(['curArea', 'user'])
  },
  created () {
    this.handleGovCapitalSubsidyProjectList()
  },
  mounted () {},
  methods: {
    async handleGovCapitalSubsidyProjectRevoke (id) {
      this.$confirm('此操作将撤销该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await govCapitalSubsidyProjectRevoke({
            id,
            areaId: this.curArea.id
          })
          this.handleGovCapitalSubsidyProjectList()
          this.$message({
            type: 'success',
            message: '撤销成功!'
          })
        })
        .catch((err) => {
          console.log(err)
        })
    },
    compareDates (time) {
      // 给定的日期时间字符串
      const givenDateTimeString = `${time} 23:59:59`

      // 将给定的日期时间字符串转换为 Date 对象
      // 注意：JavaScript 的 Date 解析 ISO 8601 格式的日期时间，所以需要将空格替换为 'T'
      const givenDate = new Date(givenDateTimeString.replace(' ', 'T'))

      // 获取当前日期和时间
      const currentDate = new Date()

      // 比较两个日期
      if (currentDate > givenDate) {
        return false
      } else {
        return true
      }
    },
    async handleGovCapitalSubsidyProjectList () {
      const res = await govCapitalSubsidyProjectList({
        pageNum: this.pagination.pageNum,
        pageSize: this.pagination.pageSize,
        name: this.searchValue.realName,
        areaId: this.curArea.id
      })
      this.tableData = res.records
      this.tableData.map((item) => {
        item.isCXShow = this.compareDates(item.endTime)
        return item
      })
      this.total = Number(res.total)
    },
    handleInfo (item, type) {
      type === 1
        ? this.$router.push(`/government/subsidyDetail?id=${item.id}`)
        : this.$router.push(
          `/government/subsidyAdd?type=${item.type}&id=${item.capitalSubsidyProjectId}&listid=${item.id}`
        )
    },
    onSubmit () {},
    getTableList () {},
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagination.pageSize = val
      this.handleGovCapitalSubsidyProjectList()
    },

    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.pagination.pageNum = val
      this.handleGovCapitalSubsidyProjectList()
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-drawer__header {
  color: #000 !important;
  font-size: 18px !important;
}

.header-search {
  display: flex;
  margin-bottom: 30px;

  .select-box {
    width: 20%;
    margin: 0 20px;
  }
}

.drawer_box {
  width: 100%;
  padding: 0px 50px;
  .drawer_item {
    display: flex;
    justify-content: flex-start;
    margin-bottom: 20px;
    padding: 10px;
  }
}
</style>
